package com.yj.web.controllers.admin;

import com.yj.web.annotation.LoginRequired;
import com.yj.web.dao.po.admin.CheckSuggestion;
import com.yj.web.dao.po.admin.FundraisingProject;
import com.yj.web.dao.po.admin.InvestRecord;
import com.yj.web.dao.po.admin.RepayPlan;
import com.yj.web.model.BaseWebResult;
import com.yj.web.service.FundraisingProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by yanlinyang on 2017/7/4.
 */
@Controller
@RequestMapping("/admin")
public class FundraisingProjectController {

    @Autowired
    private FundraisingProjectService fundraisingProjectService;

    /**
     *
     * @param statuses 筹款项目审批:[1,2,3]  在筹项目管理:[3,5,11]  不能为空
     * @param borrower_name
     * @param borrower_phone
     * @param startTime
     * @param endTime
     * @param page
     * @param size
     * @return
     */
    @LoginRequired
    @RequestMapping(value = "/listFundraisingProject", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult listFundraisingProject(@RequestParam("statuses") List<Integer> statuses,
                                                              @RequestParam("borrower_name") String borrower_name,
                           @RequestParam("borrower_phone") String borrower_phone,
                           @RequestParam("startTime") String startTime,
                           @RequestParam("endTime") String endTime,
                           @RequestParam(value = "page", defaultValue = "1") int page,
                           @RequestParam(value = "size", defaultValue = "10") int size) {
          BaseWebResult result = new BaseWebResult();
        List<FundraisingProject> projects;
        try {
            projects = fundraisingProjectService.listFundraisingProject(statuses, borrower_name, borrower_phone, startTime, endTime, page, size);
            result.setData(projects);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/countFundraisingProject", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult countFundraisingProject(@RequestParam("statuses") List<Integer> statuses,
                                                               @RequestParam("borrower_name") String borrower_name,
                                                   @RequestParam("borrower_phone") String borrower_phone,
                                                   @RequestParam("startTime") String startTime,
                                                   @RequestParam("endTime") String endTime) {
        BaseWebResult result = new BaseWebResult();
        int count = fundraisingProjectService.countFundraisingProject(statuses, borrower_name, borrower_phone, startTime, endTime);
        result.setData(count);
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/findFundraisingProjectById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult findFundraisingProjectById(@RequestParam("id") String id) {
        BaseWebResult result = new BaseWebResult();
        try {
            FundraisingProject fundraisingProject = fundraisingProjectService.findFundraisingProjectById(id);
            result.setData(fundraisingProject);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/findInvestRecordByProjectId", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult findInvestRecordByProjectId(@RequestParam("projectId") String projectId, @RequestParam(value = "page", defaultValue = "1") int page,
                                                                   @RequestParam(value = "size", defaultValue = "10") int size) {
        BaseWebResult result = new BaseWebResult();
        try {
            List<InvestRecord> investRecords = fundraisingProjectService.findInvestRecordByProjectId(projectId, page, size);
            result.setData(investRecords);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/countInvestRecordByProjectId", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult countInvestRecordByProjectId(@RequestParam("projectId") String projectId) {
        BaseWebResult result = new BaseWebResult();
        try {
            int investRecordsNum = fundraisingProjectService.countInvestRecordByProjectId(projectId);
            result.setData(investRecordsNum);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/findRepayPlanByProjectId", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult findRepayPlanByProjectId(@RequestParam("projectId") String projectId, @RequestParam(value = "page", defaultValue = "1") int page,
                                                                   @RequestParam(value = "size", defaultValue = "10") int size) {
        BaseWebResult result = new BaseWebResult();
        try {
            List<RepayPlan> repayPlen = fundraisingProjectService.findRepayPlanByProjectId(projectId, page, size);
            result.setData(repayPlen);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/countRepayPlanByProjectId", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult countRepayPlanByProjectId(@RequestParam("projectId") String projectId) {
        BaseWebResult result = new BaseWebResult();
        try {
            int repayPlenNum = fundraisingProjectService.countRepayPlanByProjectId(projectId);
            result.setData(repayPlenNum);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/terminateProject", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult terminateProject(@RequestParam("projectId") String projectId) {
        BaseWebResult result = new BaseWebResult();
        try {
            boolean flag = fundraisingProjectService.terminateProject(projectId);
            if (false == flag) {
                result.setCd(1);
                result.setMsg("系统错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/confirmLoan", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult confirmLoan(@RequestParam("projectId") String projectId) {
        BaseWebResult result = new BaseWebResult();
        try {
            boolean flag = fundraisingProjectService.confirmLoan(projectId);
            if (false == flag) {
                result.setCd(1);
                result.setMsg("系统错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/splitByAccount", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult splitByAccount(@RequestParam("projectId") String projectId, @RequestParam("period") int period) {
        BaseWebResult result = new BaseWebResult();
        try {
            boolean flag = fundraisingProjectService.splitByAccount(projectId, period);
            if (false == flag) {
                result.setCd(1);
                result.setMsg("系统错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/checkProject", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult checkProject(@ModelAttribute("suggestion") CheckSuggestion suggestion) {
        BaseWebResult result = new BaseWebResult();
        try {
            fundraisingProjectService.checkProject(suggestion);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @RequestMapping(value = "/hideDisplayProject", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult hideDisplayProject(@RequestParam("id") String id, @RequestParam("hide_flag") int hide_flag) {
        BaseWebResult result = new BaseWebResult();
        try {
            fundraisingProjectService.hideDisplayProject(id, hide_flag);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

//    @RequestMapping(value = "/displayProject", method = RequestMethod.POST)
//    public @ResponseBody BaseWebResult displayProject(@RequestParam("id") String id) {
//        BaseWebResult result = new BaseWebResult();
//        try {
//            fundraisingProjectService.displayProject(id);
//        } catch (Exception e) {
//            e.printStackTrace();
//            result.setCd(1);
//            result.setMsg("系统错误");
//        }
//        return result;
//    }

    //短信提醒用户还款
    @RequestMapping(value = "/remindRepay", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult remindRepay(@RequestParam("projectId") String projectId, @RequestParam("phone") String phone) {
        BaseWebResult result = new BaseWebResult();
        try {
            fundraisingProjectService.remindRepay(projectId, phone);
            result.setMsg("提示还款");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }
}
